package com.jkb.trainjieba;


import java.io.IOException;

import java.util.HashSet;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * 
 * @author wangbin
 * 功能：根据SplitWordsMR结果，计算词对应的IDF
 * 输入：词\t\t\t文章类别\t\t\turl
 * 输出：词\tIDF(log(总文章数量/词出现的文章数量))
 * 运行步骤：第三步
 */
public class ObtainIDFTable {

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		//args0:dst
		//args1:out
		//args2:splitMB
		//args3:reduce num
		//args4:文章总数量
		if(args.length != 5){
			System.out.println("args0:dst");
			System.out.println("args1:out");
			System.out.println("args2:splitMB");
			System.out.println("args3:reduce num");
			System.out.println("args4:文章总数量");
			System.exit(0);
		}
		int SplitMB = Integer.valueOf(args[2]);
		String dst = args[0];
		String out = args[1];
		Configuration conf = new Configuration();
		conf.set("total_article_number", args[4]);
		conf.set("mapreduce.input.fileinputformat.split.maxsize", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapred.min.split.size", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapreduce.input.fileinputformat.split.minsize.per.node", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapreduce.input.fileinputformat.split.minsize.per.rack", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapred.job.reuse.jvm.num.tasks", "-1");
		conf.set("mapred.child.java.opts", "-Xmx350m -Xmx1024m");
		Job job = new Job(conf);
		FileInputFormat.addInputPath(job, new Path(dst));
		FileOutputFormat.setOutputPath(job, new Path(out));
		
		job.setMapperClass(IDFMap.class);
		job.setReducerClass(IDFReduce.class);
		
		job.setNumReduceTasks(Integer.valueOf(args[3]));
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(DoubleWritable.class);
		
		job.setJarByClass(ObtainIDFTable.class);
        job.waitForCompletion(true);

	}
	
	public static class IDFMap extends Mapper<Object,Text,Text,Text>{
		
		public final static String SP = "\t\t\t";
		
		@Override
		public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
			String[] line = value.toString().split(SP);
			if(line.length >= 3){
				String word = line[0];
				String url = line[2];				
				context.write(new Text(word),new Text(url));
			}
		}
	}
	
	public static class IDFReduce extends Reducer<Text,Text,Text,DoubleWritable>{
		
		public final static String SP = "\t";
		
		double total = 0;
		
		@Override
		public void setup(Context context){
			total = Double.valueOf(context.getConfiguration().get("total_article_number")).doubleValue();
		}
		
		@Override
		public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException {
			HashSet<String> set = new HashSet<String>();
			for(Text v : values){	
				set.add(v.toString());
			}
			double number = (double)set.size();//词所在文章数
			double IDF = Math.log(total/number);
			context.write(key,new DoubleWritable(IDF));
		}		
	}

}
